package com.san.common.utils;

import java.io.UnsupportedEncodingException;

import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.springframework.util.DigestUtils;

public class MD5Utils {

	private static final String ALGORITH_NAME = "md5";

	private static final int HASH_ITERATIONS = 2;

	public static String encrypt(String pswd) {
		String newPassword = new SimpleHash(ALGORITH_NAME, pswd, ByteSource.Util.bytes(Constant.SECRET_KEY), HASH_ITERATIONS).toHex();
		return newPassword;
	}

	public static String encrypt(String username, String pswd) {
		String newPassword = new SimpleHash(ALGORITH_NAME, pswd, ByteSource.Util.bytes(Constant.SECRET_KEY + username),
				HASH_ITERATIONS).toHex();
		return newPassword;
	}
	
	/**
	 * utf8字符集格式md5加密
	 * @param content
	 * @return
	 */
	public static String MD5Encode(String content) {
		String result = null;
        try {
        	result = DigestUtils.md5DigestAsHex(content.getBytes("UTF8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
        if (null != result) {
        	return result.toUpperCase();
        }
        return result;
	}
	
	/**
	 * utf8字符集格式md5加密
	 * @param content
	 * @return
	 */
	public static String MD5Encode(String content,String charset) {
		String result = null;
        try {
        	result = DigestUtils.md5DigestAsHex(content.getBytes(charset));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
        if (null != result) {
        	return result.toUpperCase();
        }
        return result;
	}
	
}
